package com.shuoyue.ycgk.ui.recruitment.resume.major;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentTransaction;

import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.views.dialog.CustomPicker;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SelectMajorActivity extends BaseMvpActivity {

    @BindView(R.id.back)
    ImageButton back;
    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.edu)
    TextView edu;
    @BindView(R.id.major)
    EditText major;
    @BindView(R.id.search)
    Button search;
    @BindView(R.id.framLayout)
    FrameLayout framLayout;
    @BindView(R.id.backlist)
    TextView backlist;

    FragmentMajorTab fragmentMajorTab;
    FragmentSearchList fragmentSearchList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_major);
        ButterKnife.bind(this);
    }

    /**
     * 初始化layouy
     */
    @Override
    protected int getLayoutId() {
        return R.layout.activity_select_major;
    }

    /**
     * 初始化布局
     */
    @Override
    protected void initView() {
        initFragment();
    }

    @OnClick({R.id.back, R.id.edu, R.id.search, R.id.backlist})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.edu:
                showeducation();
                break;
            case R.id.search:
                searchMajor(edu.getText().toString(), major.getText().toString());
                backlist.setVisibility(View.VISIBLE);
                break;
            case R.id.backlist:
                backList();
                backlist.setVisibility(View.GONE);
                break;
        }
    }

    void initFragment() {
        fragmentSearchList = new FragmentSearchList();
        fragmentMajorTab = new FragmentMajorTab();
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.framLayout, fragmentSearchList);
        fragmentTransaction.add(R.id.framLayout, fragmentMajorTab);
        fragmentTransaction.commit();
        backList();
    }

    void backList() {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.detach(fragmentSearchList);
        fragmentTransaction.attach(fragmentMajorTab);
        fragmentTransaction.commit();
    }

    void searchMajor(String edu, String major) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.detach(fragmentSearchList);
        fragmentTransaction.attach(fragmentMajorTab);
        fragmentTransaction.commit();
        fragmentSearchList.search(edu, major);
    }

    /**
     * 学历
     */
    void showeducation() {
        String[] dataArray = new String[]{"高中", "中专", "专科", "本科", "硕士研究生", "博士研究生"};
        CustomPicker picker = new CustomPicker(this, dataArray, "最高学历");
        picker.setSelectedTextColor(ContextCompat.getColor(mContext, R.color.all_orange_color));
        picker.setUnSelectedTextColor(ContextCompat.getColor(mContext, R.color.text_main_title));
        picker.setCanceledOnTouchOutside(true);
        picker.setOffset(1);//显示的条目的偏移量，条数为（offset*2+1）
//        picker.setGravity(Gravity.CENTER);//居中
        picker.setOnItemPickListener((position, option) -> {
            picker.dismiss();
            edu.setText(option);
        });
        picker.show();
    }
}
